package com.zbkj.service.model.front.vo.user;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.zbkj.dal.entity.user.UserBalanceRecord;
import com.zbkj.service.model.front.vo.fund.MonthCrabRoeVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Data
@ApiModel
public class CurrentFundFlowVO {

    @ApiModelProperty(value = "月份", example = "2024-03")
    private String month;

    @ApiModelProperty(value = "月度流水金额")
    private MonthCrabRoeVO monthCrabRoeVO;

    @ApiModelProperty(value = "月度流水")
    private List<DateFundFlowVO> fundFlowDateGroup;

    public static CurrentFundFlowVO generate(LocalDate monthDate, List<UserBalanceRecord> userBalanceRecords) {
        MonthCrabRoeVO monthCrabRoeVO = MonthCrabRoeVO.generate(userBalanceRecords);
        CurrentFundFlowVO currentFundFlowVO = new CurrentFundFlowVO();
        currentFundFlowVO.setMonth(LocalDateTimeUtil.format(monthDate, DatePattern.NORM_MONTH_PATTERN));
        currentFundFlowVO.setMonthCrabRoeVO(monthCrabRoeVO);

        List<FundFlowVO> fundFlows = userBalanceRecords.stream().map(FundFlowVO::transform).collect(Collectors.toList());
        Map<LocalDate, List<FundFlowVO>> dateFlowMap = fundFlows.stream().collect(Collectors.groupingBy(FundFlowVO::getFlowTime));

        List<DateFundFlowVO> dateFundFlowList = new ArrayList<>();
        dateFlowMap.forEach((date, flowList) -> dateFundFlowList.add(DateFundFlowVO.generate(date, flowList)));
        currentFundFlowVO.setFundFlowDateGroup(dateFundFlowList.stream().sorted(Comparator.comparingInt(DateFundFlowVO::getIntDate).reversed()).collect(Collectors.toList()));
        return currentFundFlowVO;
    }
}
